<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.breed"
              clearable
              placeholder="请输入品种名称"
              @clear="queryData"
              @keyup.enter.native="queryData"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-plus" type="primary" @click="handleEdit"
              >添加</el-button
            >
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
    >
      <el-table-column label="品种名称" prop="breed" show-overflow-tooltip />
      <el-table-column label="品系名称" prop="strain" show-overflow-tooltip />
      <el-table-column label="批次号" prop="batchCode" show-overflow-tooltip />
      <el-table-column
        label="出雏日期"
        min-width="110"
        prop="broodingDate"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          {{
            row.broodingDate ? parseTime(row.broodingDate, "{y}-{m}-{d}") : "-"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="出栏日期"
        min-width="110"
        prop="publicationDate"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          {{
            row.publicationDate
              ? parseTime(row.publicationDate, "{y}-{m}-{d}")
              : "-"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="淘汰日期"
        min-width="110"
        prop="phaseOutDate"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          {{
            row.phaseOutDate ? parseTime(row.phaseOutDate, "{y}-{m}-{d}") : "-"
          }}
        </template>
      </el-table-column>
      <el-table-column label="数量" prop="num" show-overflow-tooltip />
      <el-table-column label="死亡数量" prop="deathNum" show-overflow-tooltip />
      <el-table-column label="单位" prop="unit" show-overflow-tooltip />
      <el-table-column label="性别" prop="sex" show-overflow-tooltip />
      <el-table-column label="日龄" prop="age" show-overflow-tooltip />
      <el-table-column label="用途" prop="use" show-overflow-tooltip />
      <el-table-column label="圈舍" prop="penHouse" show-overflow-tooltip />
      <el-table-column align="center" fixed="right" label="操作" width="220">
        <template #default="{ row }">
          <el-button type="text" @click="handleVaccineTable(row)"
            >疫苗计划</el-button
          >
          <el-button type="text" @click="handleFeedPlanTable(row)"
            >饲喂计划</el-button
          >
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <variety-desktop-edit ref="edit" @fetch-data="fetchData" />
    <vaccine-table ref="vaccineTable" @fetch-data="fetchData" />
    <feed-plan-table ref="feedPlanTable" @fetch-data="fetchData" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { API_varietyList, API_varietyDelete } from "@/api/variety";
import VarietyDesktopEdit from "./components/VarietyDesktopEdit";
import VaccineTable from "./components/VaccineTable";
import FeedPlanTable from "./components/FeedPlanTable";
import { parseTime } from "@/utils/index";
export default {
  name: "Variety",
  components: { VarietyDesktopEdit, VaccineTable, FeedPlanTable },
  data() {
    return {
      parseTime,
      list: [],
      listLoading: true,
      layout: "total, sizes, prev, pager, next, jumper",
      total: 0,
      elementLoadingText: "正在加载...",
      queryForm: {
        pageNo: 1,
        pageSize: 10,
        breed: "",
      },
      timeOutID: null,
    };
  },
  computed: {
    ...mapGetters({
      device: "settings/device",
    }),
  },
  created() {
    this.fetchData();
  },
  beforeDestroy() {
    clearTimeout(this.timeOutID);
  },
  methods: {
    handleEdit(row) {
      if (this.device === "mobile") {
        return this.$router.push({
          name: "VarietyMobileEdit",
          params: { ...row },
        });
      }
      if (row.code) {
        this.$refs["edit"].showEdit(row);
      } else {
        this.$refs["edit"].showEdit();
      }
    },
    handleVaccineTable(row) {
      if (row.code) {
        this.$refs["vaccineTable"].showEdit(row);
      } else {
        this.$refs["vaccineTable"].showEdit();
      }
    },
    handleFeedPlanTable(row) {
      if (row.code) {
        this.$refs["feedPlanTable"].showEdit(row);
      } else {
        this.$refs["feedPlanTable"].showEdit();
      }
    },
    handleDelete(row) {
      if (row.code) {
        this.$baseConfirm("你确定要删除当前项吗", null, async () => {
          const { message } = await API_varietyDelete({ code: row.code });
          this.$baseMessage(message, "success");
          this.fetchData();
        });
      }
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val;
      this.fetchData();
    },
    queryData() {
      this.queryForm.pageNo = 1;
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await API_varietyList(this.queryForm);
      this.list = data.list;
      this.total = data.count;
      this.timeOutID = setTimeout(() => {
        this.listLoading = false;
      }, 300);
    },
  },
};
</script>
